﻿@{
    Layout = null;
}
@model Freshbox2._0.MODEL.storediscount
<!DOCTYPE html>

<html>
<head>
    <link rel="shortcut icon" href="favicon.ico">
    <link rel="icon" type="image/gif" href="~/img/animated_favicon1.gif">
    <meta name="viewport" content="width=device-width" />
    <link href="~/UILibs/easyui/themes/default/easyui.css" rel="stylesheet" />
    <link href="~/UILibs/easyui/themes/icon.css" rel="stylesheet" />
    <link href="~/Css/css.css" rel="stylesheet" />

    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
    <script src="~/UILibs/easyui/jquery.easyui.min.js"></script>
    <script src="~/UILibs/easyui/locale/easyui-lang-zh_CN.js"></script>

    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
    <title></title>
    <script type="text/javascript">

        //时间格式化 转化为 yyyy
        function format(str) {

            if (str != null) {
                var d = eval('new ' + str.substr(1, str.length - 2));
                var ar_date = [d.getFullYear(), d.getMonth() + 1, d.getDate()];
                for (var i = 0; i < ar_date.length; i++) ar_date[i] = dFormat(ar_date[i]);
                return ar_date.join('-');
                function dFormat(i) { return i < 10 ? "0" + i.toString() : i; }
            }
            else {
                return null;
            }
        }

        $(function () {
            
            $("#storeName").combobox({
                url: '/OrderStatistic/GetShopName',
                //action 
                valueField: "shopID",
                textField: "shopName"
            });

            $("#txtName").combobox({
                url: '/OrderStatistic/GetShopName',
                //action 
                valueField: "shopID",
                textField: "shopName"
            });

            $("#data").datagrid({
                fit: true,
                title: '店铺满减优惠列表',
                striped: true,
                rownumbers: true,
                columns:
                    [[{
                        field: 'ck',
                        checkbox: true
                    }, {
                        field: 'storeDiscountId',//storeDiscountId storeId value type storeName startTime   engTime  status
                        title: '优惠编号',
                        width: 100,
                        height: 50,
                        halign: 'center',
                        sortable: true,
                        align: 'center'
                    }, {
                        field: 'storeId',
                        title: '店铺ID',
                        width: 150,
                        height: 50,
                        halign: 'center',
                        align: 'center',

                    }, {
                        field: 'storeName',
                        title: '店铺名称',
                        width: 200,
                        height: 50,
                        halign: 'center',
                        align: 'center'
                    }, {
                        field: 'value',
                        title: '折扣',
                        width: 100,
                        height: 50,
                        halign: 'center',
                        align: 'center'
                    }, {
                        field: 'startTime',
                        title: '开始时间',
                        width: 150,
                        height: 50,
                        halign: 'center',
                        align: 'center',
                        formatter: format
                    }, {
                        field: 'engTime',
                        title: '结束时间',
                        width: 150,
                        height: 50,
                        halign: 'center',
                        align: 'center',
                        formatter: format
                    }, {
                        field: 'status',
                        title: '标志',
                        width: 100,
                        height: 50,
                        halign: 'center',
                        align: 'center'
                    }]],
                url: pageConfig.getAction,
                pagination: true,
                pageSize: 10,
                pageList: [ 20, 25,30],
                sortName: 'adminId',
                sortOrder: 'desc',
                toolbar: '#tool'
            });

            $("#formdialog").dialog({
                width: 400,
                height: 400,
                closed: true,
                modal: true,
                buttons: '#btns'
            });

        });
        var pageConfig = {
            type: null,
            getAction: '@Url.Action("GetAll")',
            saveAction: '@Url.Action("Save")',
            delAction: '@Url.Action("ModifyState")',
            modifyAction: '@Url.Action("Modify")',
            getOneAction: '@Url.Action("GetOnlyOne")',
        };
        ///禁用用户   状态改变
        function del() {

            var rows = $("#data").datagrid("getSelections")
            if (rows.length <= 0) {
                $.messager.alert("删除优惠", "没有选中的行", "info");
                return;
            }

            $.messager.confirm("确定操作", "您确定要删除该优惠吗？", function (flag) {
                if (flag) {
                    var idsStr = rows[0].storeDiscountId;
                    $.ajax({
                        url: pageConfig.delAction,
                        data: { id: idsStr },
                        dataType: "json",
                        type: "POST",
                        beforeSend: function () {
                            before();
                        },
                        success: function (data) {
                            success(data);
                        },
                        error: function () {
                            error();
                        },
                        complete: function () {
                            complete();
                        }
                    });
                }
            })
        }
        //弹出框
        function alertFormDialog(type) {
            pageConfig.type = type;
            $("#formdialog form").form("reset");
            if (type == 1) {
                $("#formdialog").dialog("open");
                $("#formdialog").dialog("setTitle", "添加");
                $("#formdialog form").attr("action", pageConfig.saveAction);
            }
            else {
                var rows = $("#data").datagrid("getSelections");

                if (rows.length != 1) {
                    $.messager.alert("提示", "修改时必须且只能选中一条记录！", "info");
                }
                if (rows[0].state == 1) {
                    $.messager.alert("提示", "此优惠已经无效！不能进行修改", "info");
                }
                else {

                    $("#formdialog").dialog("open");
                    $("#formdialog").dialog("setTitle", "修改");
                    //$('#adminUsername').attr("disabled", true);//不可编辑
                    $.ajax({
                        async: false,
                        url: pageConfig.getOneAction + "?id=" + rows[0].storeDiscountId,
                        dataType: "json",
                        type: "POST",
                        success: function (data) {
                            alert(data.value);
                            $("#formdialog input[name='storeDiscountId']").val(data.storeDiscountId);
                            $("#storeName").combobox('setText', data.storeName),                          
                            $("#storeId").val(data.storeId);
                            $("#value").val(data.value);
                            $("#startTime").datebox('setValue', data.startTime);
                            $("#engTime").datebox('setValue', data.engTime);
                        }
                    });
                }
            }
        }

        function closeDptDialog() {
            $("#formdialog").dialog("close")
        }
        //
        //点击保存按钮  storeId engTime   storeDiscountId storeName startTime  cutMoney1  cutMoney2  fullMoney1 fullMoney2 status
        function save() {

            if ($("#value").val() != null && $("#engTime").val() != "") {
                var bool = $("#fm").form("validate");
                if (bool) {
                    var rows = $("#data").datagrid("getSelections");
                    var data;
                    if (pageConfig.type == 1) {
                        url = pageConfig.saveAction;
                        data = {
                            storeDiscountId: $("#formdialog input[name='storeDiscountId']").val(),//$("#shopName").combobox('getValue'),
                            storeId: $("#storeName").combobox('getValue'),
                            value: $("#value").val(),               
                            startTime: $("#startTime").val(),
                            endTime: $("#engTime").val(),
                        };
                    }
                    else {
                        url = pageConfig.modifyAction;
                        data = {
                            storeDiscountId: $("#formdialog input[name='storeDiscountId']").val(),//$("#shopName").combobox('getValue'),
                            storeId: $("#storeId").val(),
                            value: $("#value").val(),           
                            startTime: $("#startTime").val(),
                            endTime: $("#engTime").val(),
                        };
                    }
                    $.ajax({
                        url: url,
                        dataType: "json",
                        data: data,
                        type: "POST",
                        beforeSend: function () {
                            $.messager.progress({ title: "执行中", text: "" });
                        },
                        success: function (data) {
                            $.messager.alert("成功", data.msg, "info");
                        },
                        error: function (data) {
                            $.messager.alert("警告", "系统故障", "error", "")
                        },
                        complete: function () {

                            complete();
                        }
                    });

                }
            }
            else {
                $.messager.alert("信息不能为空", "信息不能为空", "info");
            }
        }

        function searchData() {
            $("#data").datagrid('load', {
                storeId: $("#txtName").val()

            });
        }

        function before() {
            $.messager.progress({ title: "执行中", text: "" });
        }

        function success(data) {
            $.messager.alert("成功", data.msg, "info");
        }
        function error() {

            $.messager.alert("警告", "系统故障", "error", "")
        }

        function complete() {
            $("#data").datagrid("load");
            $.messager.progress("close");
            $("#formdialog").dialog("close");
        }

    </script>
</head>
<body class="easyui-layout" style="width: 100%; height: 100%">
    <div data-options="region:'center'">
        <table id="data"></table>
        <div id="tool" style="padding: 10px;">
            <div style="margin-bottom: 10px;">
                <div style="margin-bottom: 10px;">
                    &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp 
                    <a class="easyui-linkbutton" iconcls="icon-add" style="width: 100px; height: 50px;" onclick="alertFormDialog(1)">添加优惠</a> &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp 
                    <a class="easyui-linkbutton" iconcls="icon-edit" style="width: 100px; height: 50px;" onclick="alertFormDialog(2)">修改优惠</a> &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp 
                    <a class="easyui-linkbutton" iconcls="icon-remove" style="width: 100px; height: 50px;" onclick="del()">删除优惠</a>
                    &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp 
                店铺名称：<input id="txtName" style="width: 180px; height: 25px; margin-top: 15px" type="text" class="easyui-combobox" oninput="searchData()" />
                    &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp 
                <a class="easyui-linkbutton" iconcls="icon-search" onclick="searchData()">查询</a>
                </div>
            </div>
        </div>
    </div>

    <div id="formdialog">
        @using (Ajax.BeginForm(new AjaxOptions
        {
            OnBegin = "before",
            OnComplete = "complete",
            OnSuccess = "success",
            OnFailure = "error",
            HttpMethod = "Post"
        }))
        {
            //storeDiscountId   storeName  engTime   startTime  cutMoney1  cutMoney2  fullMoney1 fullMoney2 status
            <div class="dialog-table">
                <form id="fm" method="post">

                    @Html.HiddenFor(x => x.storeDiscountId)

                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tk_table">

                        <tr>
                            <td>店铺名称：</td>
                            <td class="white-tr">
                                <input class="easyui-combobox" id="storeName" data-options="required:true" /></td>

                        </tr>@* //storeDiscountId storeId value type storeName startTime   engTime  status*@
                        <tr hidden="hidden">
                            <td>店铺ID：</td>
                            <td class="white-tr">
                                <input class="easyui-validatebox" id="storeId"   hidden="hidden" /></td>
                        </tr>
                        <tr>
                            <td>折扣：</td>
                            <td class="white-tr">
                                <input class="easyui-validatebox" id="value" data-options="required:true" /></td>
                        </tr>
        
                        <tr>
                            <td>开始时间：</td>
                            <td class="white-tr">
                                <input class="easyui-datebox" id="startTime" data-options="required:true" /></td>
                        </tr>
                        <tr>
                            <td>结束时间：</td>
                            <td class="white-tr">
                                <input class="easyui-datebox" id="engTime" data-options="required:true" /></td>
                        </tr>
                    </table>
                </form>
            </div>
        }
    </div>

    <div id="btns">
        <div style="margin-bottom: 10px">
            <a class="easyui-linkbutton" iconcls="icon-redo" onclick="closeDptDialog()">取消</a>
            <a class="easyui-linkbutton" iconcls="icon-add" onclick="save()">保存</a>
        </div>
    </div>

</body>
</html>
